package net.diaowen.dwsurvey.service.impl;



import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.diaowen.common.base.entity.User;
import net.diaowen.common.base.entity.WcUserDetail;
import net.diaowen.common.base.service.AccountManager;
import net.diaowen.common.plugs.page.Page;
import net.diaowen.common.plugs.page.PropertyFilter;
import net.diaowen.common.service.BaseServiceImpl;
import net.diaowen.common.utils.Arithmetic;
import net.diaowen.dwsurvey.dao.LotteryPrizeArithmeticDao;
import net.diaowen.dwsurvey.dao.LotteryPrizeDao;
import net.diaowen.dwsurvey.dao.LotteryPrizeDetailDao;
import net.diaowen.dwsurvey.dao.LotteryPrizeStatusDao;
import net.diaowen.dwsurvey.dao.LotteryUserDetailDao;
import net.diaowen.dwsurvey.dao.PrizeTypeDao;
import net.diaowen.dwsurvey.dao.StudentLotteryDao;
import net.diaowen.dwsurvey.entity.LotteryPrize;
import net.diaowen.dwsurvey.entity.LotteryPrizeArithmetic;
import net.diaowen.dwsurvey.entity.LotteryPrizeDetail;
import net.diaowen.dwsurvey.entity.LotteryPrizeStatus;
import net.diaowen.dwsurvey.entity.LotteryUserDetail;
import net.diaowen.dwsurvey.entity.Prize;
import net.diaowen.dwsurvey.entity.PrizeType;
import net.diaowen.dwsurvey.entity.StudentLottery;
import net.diaowen.dwsurvey.entity.StudentSurvey;
import net.diaowen.dwsurvey.service.LotteryPrizeManager;
import net.diaowen.dwsurvey.service.LotteryUserManager;
import net.diaowen.dwsurvey.service.StudentLotteryManager;
import net.diaowen.dwsurvey.service.WechatManager;

@Service
public class StudentLotteryManagerImpl extends BaseServiceImpl<StudentLottery, String> implements  StudentLotteryManager {
	
	
	@Autowired
	private AccountManager accountManager;
	
	@Autowired
	private StudentLotteryDao studentLotteryDao;

		
	@Override
	public void setBaseDao() {
		this.baseDao = studentLotteryDao;
		
	}

	@Override
	public Page<StudentLottery> findPage(Page<StudentLottery> page, StudentLottery entity) {
		
		List<Criterion> criterions=new ArrayList<Criterion>();
		
		criterions.add(Restrictions.eq("lotteryActivityId", entity.getLotteryActivityId()));
		
		String name = entity.getName();
		if(name != null && !name.equals("")) {
			criterions.add(Restrictions.like("name", "%"+name+"%"));
		}
		
		String studentId = entity.getStudentId();
		if(studentId != null && !studentId.equals("")) {
			criterions.add(Restrictions.like("studentId", "%"+studentId+"%"));
		}
		
		String prizeTypeName = entity.getPrizeTypeName();
		if(prizeTypeName != null && !prizeTypeName.equals("")) {
			criterions.add(Restrictions.like("prizeTypeName", "%"+prizeTypeName+"%"));
		}
		
		
		String approvalStatus = entity.getApprovalStatus();
		if(approvalStatus != null && !approvalStatus.equals("")) {
			criterions.add(Restrictions.like("approvalStatus", "%"+approvalStatus+"%"));
		}
		
		User curUser = accountManager.getCurUser();
		//如果是超级管理员则不加入机构ID查询，其余管理员加入机构ID
		if (curUser.getSystemRole() == 8) {
			//
		} else {
			criterions.add(Restrictions.eq("orgId", curUser.getOrgId()));
		}
		
		criterions.add(Restrictions.ne("prizeTypeId", "1"));
		page.setOrderBy("lotteryTime");
		page.setOrderDir("desc");
		return super.findPageByCri(page, criterions);
	}

	@Override
	public List<StudentLottery> findListByWxid(String wxid) {
		List<Criterion> criterions=new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("wxId", wxid));
		criterions.add(Restrictions.ne("prizeTypeId", "1"));
		return studentLotteryDao.findAll(criterions, Order.desc("lotteryTime"));
	}

}
